Xceed DataGrid for Silverlight Documentation
Cell Editors

A cell editor is a control that is used to edit the content of editable cells. Each column's data type determines which cell editor will be used, by default, to edit its cells' content (see Table 1); however, through the CellEditorStyle property, the default cell editors can be restyled.  

Table 1: Default cell editors

Default Cell Editor Supported Data Types
DataGridCheckBox Boolean
Nullable<Boolean>
DatePicker DateTime
Nullable<DateTime>
DataGridNumericTextBox Int16, Int32, Int64, UInt32, UInt64, Single, Double, Decimal
Nullable<Int16>, Nullable<Int32>, Nullable<Int64>, Nullable<UInt32>, Nullable<UInt64>, Nullable<Single>, Nullable<Double>, Nullable<Decimal>
DataGridTextBox String
DataGridComboBox Enum (only if the enumeration does NOT have the FlagAttribute)

Default cell editors will not be automatically detected for data sources that do not provide type information (e.g., non-IEnumerable<T>) or for anonymous types.

Cell-Editor Display Conditions and Edit Triggers

How and when the data in a grid can be edited is determined through combinations of the EditTriggers and CellEditorDisplayConditions properties, which state what triggers place a grid in edit mode and when the editors, which are used to edit cell content, are displayed, respectively.  By default, the EditTriggers property is set to ClickOnCurrentCell, meaning that a grid will enter edit mode when the current cell is clicked, while the CellEditorDisplayConditions property is set to None, indicating that the current cell's editor will only be displayed when it is being edited. This combination of values results in a double-click action being necessary to edit the content of a cell.

Both the EditTriggers and CellEditorDisplayConditions properties support bitwise combinations of their member values and can be set on the grid or on a per-column basis.

Using the DataGridComboBox

In order to use the DataGridComboBox cell editor, it must be explicitly defined through a column's CellEditorTemplate property. In most circumstances, the combo-box cell editor will be used to display foreign-key values (i.e., key/value pairs), in which case it becomes necessary to provide various templates to display the data.

A column's ActivatingCellEditor event is raised whenever a cell editor is about to be activated to allow custom settings to be provided.

   <sldg:DataGridControl ItemsSource="{Binding Path=Orders}">
      <sldg:DataGridControl.Resources>
         <Style x:Key="customerViewerStyle"
                TargetType="TextBlock">
            <Setter Property="TextTrimming" Value="WordEllipsis" />
            <Setter Property="VerticalAlignment" Value="Center" />
         </Style>
         
         <!-- DataTemplate to display a Customer -->
         <DataTemplate x:Key="customerViewerDataTemplate">
            <TextBlock Text="{Binding Customer.CompanyName}"
                       Style="{StaticResource customerViewerStyle}" />
         </DataTemplate>
         
         <!-- DataTemplate for the items of the ComboBox -->
         <DataTemplate x:Key="customerEditorItemTemplate">
            <TextBlock Text="{Binding CompanyName}"
                       Style="{StaticResource customerViewerStyle}" />
         </DataTemplate>
 
         <!-- DataTemplate to modify a Customer -->
         <DataTemplate x:Key="customerEditorDataTemplate">
            <sldg:DataGridComboBox ItemTemplate="{StaticResource customerEditorItemTemplate}" />
         </DataTemplate>
      </sldg:DataGridControl.Resources>
      <sldg:DataGridControl.Columns>
         <sldg:Column FieldName="Customer"
                      Title="Customer"
                      CellContentTemplate="{StaticResource customerViewerDataTemplate}"
                      CellEditorTemplate="{StaticResource customerEditorDataTemplate}"
                      ActivatingCellEditor="CustomerColumn_ActivatingCellEditor" />
      </sldg:DataGridControl.Columns>
   </sldg:DataGridControl>
 
Private Sub CustomerIDColumn_ActivatingCellEditor( ByVal sender As Object, ByVal e As ActivatingCellEditorEventArgs )
   Dim editor As DataGridComboBox = CType( e.CellEditor, DataGridComboBox )
 
   If editor Is Nothing Then
      Return
   End If
 
   editor.ItemsSource = SampleDataProvider.SharedCustomers
End Sub
private void CustomerIDColumn_ActivatingCellEditor( object sender, ActivatingCellEditorEventArgs e )
{
   DataGridComboBox editor = e.CellEditor as DataGridComboBox;
 
   if( editor == null )
      return;
 
   editor.ItemsSource = SampleDataProvider.SharedCustomers;
} 

Custom Cell Editors

In addition to the default cell editors, it is also possible to define custom cell editors by specifying, as a DataTemplate, the editor (i.e., FrameworkElement) to be used through the column's CellEditorTemplate property (see also the CellContentTemplate property for information on how to provide the same control to view the content rather than edit it).

When defining a custom cell editor, the first FrameworkElement in the DataTemplate will be used as the editor and the grid will automatically bind to the appropriate property, depending on the element being used (see table below). If a custom editor that does not derive from one of the types defined in the table below is required, a ContentControl whose ContentTemplate is defined can be used (see RatingCellEditor class). In this case, the grid will automatically bind to the ContentControl's Content property using a TwoWay binding. The DataContext of the element used as an editor is the data object (e.g., Person, Order, Customer); therefore, the editor can be bound to a child property of the object, if required.

Editor Property
DataGridNumericTextBox Value
DataGridTextBox/TextBox Text
DataGridCheckBox/ToggleButton (CheckBox, RadioButton) IsChecked
DataGridComboBox/Selector (ComboBox, ListBox) SelectedValue
DatePicker SelectedDate
ContentControl Content

The code for the RatingCellEditor class can be found in the Additional Resources section of the documentation.

 

Send Feedback